package com.ganzalang.gmall.designpatterns.adapter;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: 使用适配器模式
 * @author: ganzalang
 */
@Slf4j
public class AdapterPatternDemo {

    /**
     * 适配器模式就是将新老接口统一起来，比如老接口是oldExecute()，新接口是newExecute()，使用适配器模式
     * 将新老接口都统一成newExecute()。
     * @param args
     */
    public static void main(String[] args) {
        AdapterNewInterface adapterNewInterface = new AdapterNewInterface(new OldInterfaceImpl());
        NewInterface newInterface = new NewInterfaceImpl();
        adapterNewInterface.newExecute();
        newInterface.newExecute();
    }

    public interface OldInterface {
        void oldExecute();
    }

    public interface NewInterface {
        void newExecute();
    }

    public static class AdapterNewInterface implements NewInterface {

        private OldInterface oldInterface;

        public AdapterNewInterface(OldInterface oldInterface) {
            this.oldInterface = oldInterface;
        }

        @Override
        public void newExecute() {
            oldInterface.oldExecute();
        }
    }

    public static class OldInterfaceImpl implements OldInterface {

        @Override
        public void oldExecute() {
            log.info("老的方法");
        }
    }

    public static class NewInterfaceImpl implements NewInterface {

        @Override
        public void newExecute() {
            log.info("新的方法");
        }
    }
}
